package com.newenergy.wisehome.gateway.account;


import android.os.AsyncTask;
import android.support.design.widget.TextInputEditText;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.newenergy.wisehome.R;
import com.newenergy.wisehome.gateway.common.BLCommonUtils;
import com.newenergy.wisehome.ui.fragment.BaseFragment;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.broadlink.account.BLAccount;
import cn.com.broadlink.base.BLAppSdkErrCode;
import cn.com.broadlink.base.BLBaseResult;

/**
 * A simple {@link Fragment} subclass.
 */
public class ForgetPasswordFragment extends BaseFragment {

    @BindView(R.id.et_name)
    TextInputEditText mEtName;
    @BindView(R.id.btn_next)
    Button mBtnNext;
    @BindView(R.id.tv_page)
    TextView mTvPage;


    //protected  String mUserName;
    @Override
    public int getLayoutId() {
        return R.layout.fragment_forget_password;
    }

    @Override
    public void init(View rootView) {
        mTvPage.setText("找回密码");
    }


    @OnClick({R.id.btn_back, R.id.tv_back, R.id.btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                doBack();
                break;
            case R.id.tv_back:
                doBack();
                break;
            case R.id.btn_next:
                String userName = mEtName.getText().toString();
                if (checkUserName(userName)) {
                    new RequestVerifyCodeTask().execute(userName);
                }
                break;
        }


    }

    //检测用户名称格式是否正确，如果出现不在0-9的字符，则判断为邮箱格式
    private boolean checkUserName(String userName) {
        //检测是否为空
        if (TextUtils.isEmpty(userName)) {
            toast("用户名不能为空");
            return false;
        }

        if (!TextUtils.isDigitsOnly(userName) && !BLCommonUtils.isEmail(userName)) {
            toast("用户名必须是手机号或邮箱");
            return false;
        }
        return true;
    }

    /**
     * 获取发送验证码线程
     **/
    class RequestVerifyCodeTask extends AsyncTask<String, Void, BLBaseResult> {

        String userName;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected BLBaseResult doInBackground(String... params) {
            userName = params[0];
            return BLAccount.sendRetrieveVCode(userName);
        }

        @Override
        protected void onPostExecute(BLBaseResult result) {
            super.onPostExecute(result);
            if (result != null && result.getError() == BLAppSdkErrCode.SUCCESS) {
                start(ResetPasswordFragment.newInstance(userName));
            } else if (result != null) {
                String msg = "找回密码失败: error = " + result.getError() + ", msg  = " + result.getMsg();
                Log.e(TAG, msg);
                toast(msg);
            } else {
                String msg = "找回密码失败:网络错误";
                Log.e(TAG, msg);
                toast(msg);
            }
        }
    }

}
